/*
 * Copyright (C) 2012 Sreekumar SH
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.gravata.enigma.dragdrop.listview;

import java.util.List;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import com.gravata.enigma.R;
import com.gravata.enigma.model.Bouquet;
import com.gravata.enigma.model.BouquetService;
/**
 * Adapter for the drag and drop listview
 * @author <a href="http://sreekumar.sh" >Sreekumar SH </a> (sreekumar.sh@gmail.com)
 *
 */
public final class DragNDropAdapter extends BaseExpandableListAdapter implements RemoveListener, DropListener{

	private int[] mIds;
    private int[] mLayouts;
    private Context mContext;
    private LayoutInflater mInflater;
    private List<Bouquet> groups;

    public DragNDropAdapter(Context context, List<Bouquet> group) {
        init(context,new int[]{android.R.layout.simple_list_item_1},new int[]{android.R.id.text1}, group);
    }

    public DragNDropAdapter(Context context, int[] itemLayouts, int[] itemIDs, List<Bouquet> group) {
    	init(context,itemLayouts,itemIDs, group);
    }

    private void init(Context context, int[] layouts, int[] ids, List<Bouquet> group) {
    	// Cache the LayoutInflate to avoid asking for a new one each time.
    	mInflater = LayoutInflater.from(context);
    	mIds = ids;
    	mLayouts = layouts;
    	groups = group;
    	mContext = context;

    }

    public void setData(List<Bouquet> groups) {
		this.groups = groups;
		notifyDataSetChanged();

	}

    public static class ViewHolder {
        public TextView text,counter;
    }

	public void onRemove(int which[]) {
		if (which[0] < 0 || which[0] > groups.size() || which[1] < 0 || which[1] > getChildrenCount(which[0])) return;
		groups.get(which[0]).getServices().remove(getKey(which));
	}

	public void onDrop(int[] from, int[] to) {
		if(to[0] > groups.size() || to[0] < 0 || to[1] < 0  )
		return;

		Bouquet bouquet=getKey(from);
		bouquet.reOrdenar(from[1], to[1]);

		notifyDataSetChanged();
	}


	private BouquetService getValue(int[] id)
	{
		return groups.get(id[0]).getService(id[1]);
	}

	private Bouquet getKey(int[] id)
	{
		return groups.get(id[0]);
	}
	@Override
	public BouquetService getChild(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return groups.get(groupPosition).getService(childPosition);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		 return childPosition;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {

		 ViewHolder holder;

	        // When convertView is not null, we can reuse it directly, there is no need
	        // to reinflate it. We only inflate a new View when the convertView supplied
	        // by ListView is null.
	        if (convertView == null) {
	            convertView = mInflater.inflate(mLayouts[0], null);

	            // Creates a ViewHolder and store references to the two children views
	            // we want to bind data to.
	            holder = new ViewHolder();
	            holder.text = (TextView) convertView.findViewById(mIds[0]);
	            holder.counter = (TextView) convertView.findViewById(R.id.txt__customizer_count);

	            convertView.setTag(holder);
	        } else {
	            // Get the ViewHolder back to get fast access to the TextView
	            // and the ImageView.
	            holder = (ViewHolder) convertView.getTag();
	        }

	        // Bind the data efficiently with the holder.
	        BouquetService bs=getChild(groupPosition, childPosition);
	        holder.text.setText(bs.getService().getName());
	        holder.counter.setText(bs.getOrdem()+"");

	        return convertView;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		try{
			return groups.get(groupPosition).getServices().size();
		}catch(Exception e){
			return 0;
		}

	}

	@Override
	public Bouquet getGroup(int groupPosition) {
		// TODO Auto-generated method stub
		return groups.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		// TODO Auto-generated method stub
		return groups.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		// TODO Auto-generated method stub
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {

		 ViewHolder holder;

	        // When convertView is not null, we can reuse it directly, there is no need
	        // to reinflate it. We only inflate a new View when the convertView supplied
	        // by ListView is null.
	        if (convertView == null) {
	            convertView = mInflater.inflate(R.layout.row_section, null);

	            // Creates a ViewHolder and store references to the two children views
	            // we want to bind data to.
	            holder = new ViewHolder();
	            holder.text = (TextView) convertView.findViewById(R.id.textView1);

	            convertView.setTag(holder);
	        } else {
	            // Get the ViewHolder back to get fast access to the TextView
	            // and the ImageView.
	            holder = (ViewHolder) convertView.getTag();
	        }

	        // Bind the data efficiently with the holder.
	        holder.text.setText(getGroup(groupPosition).getName());

	        return convertView;
	}
	public TextView getGenericView() {
        // Layout parameters for the ExpandableListView
        AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
                ViewGroup.LayoutParams.FILL_PARENT, 64);

        TextView textView = new TextView(mContext);
        textView.setLayoutParams(lp);
        // Center the text vertically
        textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
        // Set the text starting position
        textView.setPadding(55, 0, 0, 0);
        return textView;
    }
	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return true;
	}

	public List<Bouquet> getGroups() {
		return groups;
	}
}